package com.bloveoacom.weichi.fragment;

import android.os.Bundle;
import android.view.View;

import com.bloveoacom.base.BaseList2Fragment;
import com.bloveoacom.base.UserInfoBean;
import com.bloveoacom.http.HttpMethods;
import com.bloveoacom.http.RxHelper;
import com.bloveoacom.observer.BaseSubscriber2;
import com.bloveoacom.util.ActivityUtil;
import com.bloveoacom.util.Constant;
import com.bloveoacom.util.DataUtil;
import com.bloveoacom.util.GsonUtil;
import com.bloveoacom.util.NumberUtil;
import com.bloveoacom.weichi.R;
import com.bloveoacom.weichi.activity.PostDetailActivity;
import com.bloveoacom.weichi.bean.NewsPostBean;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import timber.log.Timber;

/**
 * author: yjl
 * e-mail: 1271901673@qq.com
 * time  : 2018/9/25
 * desc  :
 */
public class PublishListFragment extends BaseList2Fragment<NewsPostBean> {
    @Override
    protected int getItemLayout() {
        return R.layout.rv_item_post2;
    }
    public static PublishListFragment newInstance() {
        Bundle args = new Bundle();
        PublishListFragment fragment = new PublishListFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    protected void convertView(BaseViewHolder helper, NewsPostBean item) {
        DataUtil.setItemView2(mContext,helper,item,0);
        helper.setVisible(R.id.group2,false);
        helper.setVisible(R.id.group,true);
    }

    @Override
    protected void getData() {
        BaseSubscriber2<Object> baseSubscriber2 = HttpMethods.getInstance().getHttpService().findUserCoinList(UserInfoBean.getInstance().getToken(), "0", page, getPageSize()).compose(RxHelper.getData()).subscribeWith(new BaseSubscriber2<Object>(this) {
            @Override
            public void onNext(Object o) {
                super.onNext(o);
                String s = GsonUtil.GsonString(o);
                List<NewsPostBean> datas = GsonUtil.GsonToList(s, NewsPostBean.class);
                doData(datas);
            }
        });
        addDispose(baseSubscriber2);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.layout_refresh;
    }

    @Override
    public void getBundleExtras(Bundle extras) {

    }

    @Override
    public void initListener() {
            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Bundle bundle = new Bundle();
                    bundle.putInt(PostDetailActivity.COINID, NumberUtil.getIntByStr(datas.get(position).coinId));
                    ActivityUtil.startActivity(mContext, PostDetailActivity.class, bundle);
                }
            });
    }
}
